/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver.server;

import java.io.Serializable;
import java.util.Date;
import jeeobserver.server.Statistics.DateStatistics;
import jeeobserver.server.Statistics.NumberStatistics;
import jeeobserver.server.Statistics.SamplingStatistics;

/**
 * The Class ActionStatistics.
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public abstract class ActionStatistics extends SamplingStatistics implements Serializable {

	 private static final long serialVersionUID = 40L;

	 private final DateStatistics dateStatistics;

	 private final NumberRangeStatistics cpuNanosStatistics;

	 private final NumberRangeStatistics timeNanosStatistics;

	 private final NumberStatistics executionsStatistics;

	 private final NumberStatistics exceptionsStatistics;

	 private final NumberStatistics usersStatistics;

	 public ActionStatistics(Date date, int count, DateStatistics dateStatistics, NumberRangeStatistics cpuNanosStatistics, NumberRangeStatistics timeNanosStatistics, NumberStatistics executionsStatistics, NumberStatistics exceptionsStatistics, NumberStatistics usersStatistics) {
			super(date, count);
			this.dateStatistics = dateStatistics;

			this.cpuNanosStatistics = cpuNanosStatistics;
			this.timeNanosStatistics = timeNanosStatistics;

			this.executionsStatistics = executionsStatistics;
			this.exceptionsStatistics = exceptionsStatistics;

			this.usersStatistics = usersStatistics;
	 }

	 public ActionStatistics(Date date, ActionStatistics statistics) {

			this(date, statistics.getCount(),
							statistics.getDateStatistics(),
							statistics.getCpuNanosStatistics(),
							statistics.getTimeNanosStatistics(),
							statistics.getExecutionsStatistics(),
							statistics.getExceptionsStatistics(),
							statistics.getUsersStatistics());
	 }

	 public void addUserStatistics(NumberStatistics usersStatistics) {
			this.usersStatistics.add(usersStatistics);
	 }

	 public void add(ActionStatistics statistics) {

			super.add(statistics.getCount());

			this.cpuNanosStatistics.add(statistics.getCpuNanosStatistics());
			this.timeNanosStatistics.add(statistics.getTimeNanosStatistics());

			this.executionsStatistics.add(statistics.getExecutionsStatistics());
			this.exceptionsStatistics.add(statistics.getExceptionsStatistics());

			this.usersStatistics.add(statistics.getUsersStatistics());

			this.dateStatistics.add(statistics.getDateStatistics());
	 }

	 public DateStatistics getDateStatistics() {
			return dateStatistics;
	 }

	 public NumberRangeStatistics getCpuNanosStatistics() {
			return cpuNanosStatistics;
	 }

	 public NumberRangeStatistics getTimeNanosStatistics() {
			return timeNanosStatistics;
	 }

	 public NumberStatistics getExecutionsStatistics() {
			return executionsStatistics;
	 }

	 public NumberStatistics getExceptionsStatistics() {
			return exceptionsStatistics;
	 }

	 public NumberStatistics getUsersStatistics() {
			return usersStatistics;
	 }

	 public static final class TimeStatistics extends ActionStatistics {

			private static final long serialVersionUID = 40L;

			public TimeStatistics(Date date, ActionStatistics statistics) {
				 super(date, statistics);
			}
	 }

	 public static final class TotalStatistics extends ActionStatistics {

			private static final long serialVersionUID = 40L;

			private final int samplingPeriod;

			private final String name;

			private final NumberTrend cpuNanosTrend;

			private final NumberTrend timeNanosTrend;

			private final NumberTrend executionsTrend;

			private final NumberTrend exceptionsTrend;

			private final NumberTrend usersTrend;

			public TotalStatistics(ActionStatistics statistics, String name, int samplingPeriod) {
				 super(statistics.getDate(), statistics);

				 this.samplingPeriod = samplingPeriod;

				 this.name = name;

				 this.cpuNanosTrend = new NumberTrend();
				 this.cpuNanosTrend.add(statistics.getDate(), statistics.getCpuNanosStatistics().getSum(), statistics.getCount());
				 this.timeNanosTrend = new NumberTrend();
				 this.timeNanosTrend.add(statistics.getDate(), statistics.getTimeNanosStatistics().getSum(), statistics.getCount());

				 this.executionsTrend = new NumberTrend();
				 this.executionsTrend.add(statistics.getDate(), statistics.getExecutionsStatistics().getSum(), statistics.getCount());
				 this.exceptionsTrend = new NumberTrend();
				 this.exceptionsTrend.add(statistics.getDate(), statistics.getExecutionsStatistics().getSum(), statistics.getCount());

				 this.usersTrend = new NumberTrend();
				 this.usersTrend.add(statistics.getDate(), statistics.getUsersStatistics().getSum(), statistics.getCount());

			}

			@Override
			public void add(ActionStatistics statistics) {
				 super.add(statistics);

				 this.cpuNanosTrend.add(statistics.getDate(), statistics.getCpuNanosStatistics().getSum(), statistics.getCount());
				 this.timeNanosTrend.add(statistics.getDate(), statistics.getTimeNanosStatistics().getSum(), statistics.getCount());

				 this.executionsTrend.add(statistics.getDate(), statistics.getExecutionsStatistics().getSum(), statistics.getCount());
				 this.exceptionsTrend.add(statistics.getDate(), statistics.getExceptionsStatistics().getSum(), statistics.getCount());

				 this.usersTrend.add(statistics.getDate(), statistics.getUsersStatistics().getSum(), statistics.getCount());
			}

			public void addUserStatistics(Date date, NumberStatistics usersStatistics, int count) {
				 super.addUserStatistics(usersStatistics);
				 this.usersTrend.add(date, usersStatistics.getSum(), count);
			}

			public void calculateTrendStatistics() {
				 this.cpuNanosTrend.calculateTrendStatistics();
				 this.timeNanosTrend.calculateTrendStatistics();

				 this.executionsTrend.calculateTrendStatistics();
				 this.exceptionsTrend.calculateTrendStatistics();

				 this.usersTrend.calculateTrendStatistics();
			}

			public int getSamplingPeriod() {
				 return samplingPeriod;
			}

			public String getName() {
				 return name;
			}

			public NumberTrend getCpuNanosTrend() {
				 return cpuNanosTrend;
			}

			public NumberTrend getTimeNanosTrend() {
				 return timeNanosTrend;
			}

			public NumberTrend getExecutionsTrend() {
				 return executionsTrend;
			}

			public NumberTrend getExceptionsTrend() {
				 return exceptionsTrend;
			}

			public NumberTrend getUsersTrend() {
				 return usersTrend;
			}
	 }

	 public static final class Element extends Statistics.Element {

			private static final long serialVersionUID = 40L;

			private final String context;

			private final String project;

			private final int category;

			private final String name;

			public Element(int id, String context, String project, int category, String name) {
				 super(id);
				 this.context = context;
				 this.project = project;
				 this.category = category;
				 this.name = name;
			}

			public Element(String context, String project, int category, String name) {
				 super((context + "|" + project + "|" + category + "|" + name).hashCode());
				 this.context = context;
				 this.project = project;
				 this.category = category;
				 this.name = name;
			}

			public String getContext() {
				 return this.context;
			}

			public String getProject() {
				 return project;
			}

			public int getCategory() {
				 return category;
			}

			public String getName() {
				 return name;
			}
	 }
}
